package XMLCreator;

import java.io.*;

import javax.xml.stream.*;
import javax.xml.stream.events.*;

abstract public class XMLCreator<T> {

	private XMLEventWriter eventWriter;
	private XMLEvent end;
	private XMLEventFactory eventFactory;

	protected void createDetail(String name, Object value) throws XMLStreamException {

		XMLEventFactory eventFactory = XMLEventFactory.newInstance();
		XMLEvent end = eventFactory.createDTD("\n");
		XMLEvent tab = eventFactory.createDTD("\t");
		// Create Start node
		StartElement sElement = eventFactory.createStartElement("", "", name);
		eventWriter.add(tab);
		eventWriter.add(sElement);
		// Create Content
		Characters characters = eventFactory.createCharacters(value+"");
		eventWriter.add(characters);
		// Create End node
		EndElement eElement = eventFactory.createEndElement("", "", name);
		eventWriter.add(eElement);
		eventWriter.add(end);
	}
	
//	protected void createDetail(String name, String[] value) throws XMLStreamException {//TODO
//
//		XMLEventFactory eventFactory = XMLEventFactory.newInstance();
//		XMLEvent end = eventFactory.createDTD("\n");
//		XMLEvent tab = eventFactory.createDTD("\t");
//		// Create Start node
//		StartElement sElement = eventFactory.createStartElement("", "", name);
//		eventWriter.add(tab);
//		eventWriter.add(sElement);
//		// Create Content
//		for(int i=0;i<value.length;i++){
//			Characters characters = eventFactory.createCharacters(value[i]);
//			eventWriter.add(characters);
//		}
//		// Create End node
//		EndElement eElement = eventFactory.createEndElement("", "", name);
//		eventWriter.add(eElement);
//		eventWriter.add(end);
//	}
	
	protected void startNode(String nodeName) throws XMLStreamException{
		StartElement configStartElement = eventFactory.createStartElement("",
				"", nodeName);
		eventWriter.add(configStartElement);
		eventWriter.add(end);
	}
	
	protected void finishNode(String nodeName) throws XMLStreamException{
		eventWriter.add(eventFactory.createEndElement("", "", nodeName));
		eventWriter.add(end);
	}

	abstract protected void buildXMLInner(T item) throws XMLStreamException;

	public void build(T item,PrintWriter out) throws XMLStreamException, FactoryConfigurationError {
		// Create XMLEventWriter
		eventWriter = XMLOutputFactory.newInstance().createXMLEventWriter(out);
		
		// Create a EventFactory
		eventFactory = XMLEventFactory.newInstance();
		end = eventFactory.createDTD("\n");
		// Create and write Start Tag
		StartDocument startDocument = eventFactory.createStartDocument("UTF-8");
		eventWriter.add(startDocument);

		// Create config open tag
		StartElement configStartElement = eventFactory.createStartElement("","", "All");
		eventWriter.add(configStartElement);
		eventWriter.add(end);

		buildXMLInner(item);

		eventWriter.add(eventFactory.createEndElement("", "", "All"));
		eventWriter.add(end);

		eventWriter.add(eventFactory.createEndDocument());
		eventWriter.close();
	}
}
